package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.li.interfaces.OnItemClickListener;
import com.li.widget.DoubleTextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.model.DeviceInfo;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/13 0013
 * Description: 设备列表
 */

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceHolder> {


    private LayoutInflater mInflater;

    private OnItemClickListener mItemClickListener;
    private List<DeviceInfo> mDeviceInfoList;

    public DeviceAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_device, parent, false);
        final DeviceHolder holder = new DeviceHolder(view);
        if (mItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    if (!mDeviceInfoList.get(pos).getStatus().equals("3")) {
                        mItemClickListener.onItemClick(v, holder.getLayoutPosition());
                    }
                }
            });

        }
        return holder;
    }

    @Override
    public void onBindViewHolder(DeviceHolder holder, int position) {
        DeviceInfo info = mDeviceInfoList.get(position);
        holder.mTxtNum.setTxtRight(info.getSn());
        holder.mTxtStatus.setTxtRight(info.getStatus());
        holder.mTxtShopName.setTxtRight(info.getShop_name());
        holder.mTxtAddress.setTxtRight(info.getAddress());

        switch (info.getStatus()) {
            case "1":
                holder.mTxtStatus.setTxtRight("未使用");
                holder.mTxtStatus.setRightTxtDrawableRight(R.drawable.ic_device_state_unused);
                break;
            case "2":
                holder.mTxtStatus.setTxtRight("使用中");
                holder.mTxtStatus.setRightTxtDrawableRight(R.drawable.ic_device_state_using);

                break;
            case "3":
                holder.mTxtStatus.setTxtRight("损坏");
                holder.mTxtStatus.setRightTxtDrawableRight(R.drawable.ic_device_state_damage);

                break;
        }
    }

    @Override
    public int getItemCount() {
        return mDeviceInfoList == null ? 0 : mDeviceInfoList.size();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public void setDeviceInfoList(List<DeviceInfo> deviceInfoList) {
        mDeviceInfoList = deviceInfoList;
        notifyDataSetChanged();
    }

    static class DeviceHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_num)
        DoubleTextView mTxtNum;
        @BindView(R.id.txt_status)
        DoubleTextView mTxtStatus;
        @BindView(R.id.txt_shop_name)
        DoubleTextView mTxtShopName;
        @BindView(R.id.txt_address)
        DoubleTextView mTxtAddress;

        public DeviceHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
